﻿using System.Configuration;
using System.Web.Mvc;
using Zinnia.Common.DTO.Imp;

namespace Zinnia.Common.Web.MVC.Filter
{
    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionFilter : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new ReponseModel { code = 500, msg = filterContext.Exception.Message }
                };
            }
            else
            {
                ActionResult errorPage = new RedirectResult(ConfigurationManager.AppSettings["WebSiteErrorPage"]);
                filterContext.Result = errorPage;
            }
            filterContext.ExceptionHandled = true;
            //异步记录日志
            //LogHelper.AddLog.Error(filterContext.Controller.GetType(), string.Format("{0},{1}", filterContext.Exception.Message, filterContext.Exception.StackTrace), false);

        }
    }
}
